package com.light.mytext.kedavoicetotext

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.View
import com.light.mytext.R
import com.light.mytext.kedavoicetotext2.WebIATWS
import com.light.mytext.kedavoicetotext2.WebSockConnect
import com.light.mytext.model.PayEven
import com.light.mytext.websocket.WebSockNet
import kotlinx.android.synthetic.main.activity_voice_to_text.*
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

/**
 * 科大讯飞实时语音转文字
 */
class VoiceToTextActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_voice_to_text)
        EventBus.getDefault().register(this)
        init_Listener()
    }

    fun init_Listener() {
        //实时语音转写
        tv_start1.setOnClickListener {
            tv_start1.text = "翻译中..."
            tv_start1.isClickable = false
            Thread(Runnable() {
                run() {
                    try {
                        RTASRTest.main();
                    } catch (o: Exception) {
                        o.printStackTrace();
                    }
                }
            }).start();
        }
        tv_start2.setOnClickListener {
            Thread(Runnable() {
                run() {
                    try {
//                        WebIATWS.main()
                        WebSockNet.main()
//                        WebSockConnect.mainA()
                    } catch (o: Exception) {
                        o.printStackTrace();
                    }
                }
            }).start();
        }
    }

    @Subscribe
    public fun MessageEvent(even: PayEven) {
        if ("刷新翻译".equals(even.str1)) {
            var stringbuffer = StringBuffer("")
            var content = tv_translate.text.toString()
            stringbuffer.append(content)
            stringbuffer.append("\r\n")
            stringbuffer.append(even.str2)
            tv_translate.post {
                tv_translate.text = stringbuffer.toString()
                tv_start1.isClickable = true
                tv_start1.text="开始实时语音转写"
            }

        }
    }
}